package com.hongye.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongye.util.Utils;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * @desc 手机APP用户模型。
 * @author changpf 
 * @date 2016年9月8日11:52:02
 *
 */
public class UserAppModel extends Model<UserAppModel> {
	
	private static final long serialVersionUID = 1L;
	
	public static final UserAppModel dao=new UserAppModel();

	public UserAppModel() {
		
	}
	
	/**
	 * 检查用户是否可以登录. add by changpf at 2016年9月8日12:13:36
	 * 更新登录次数
	 * @param phone
	 * @param passwd
	 * @return
	 */
	public boolean chkAppUser(String phone,String passwd){
		boolean flag=false;
		String sql="select * from buyer a where a.phone=? and password=?";
		List<UserAppModel> lst = this.find(sql, phone, passwd);
		if(lst.size()>0){
			flag=true;
		} 
		return flag;
	}
	
	/**
	 * 检查用户是否存在。
	 * @param para
	 * @return
	 */
	public static boolean findByPhone(String phone) {
		boolean flag=false;
		String sql="select * from buyer a where a.phone=?";
		List<UserAppModel> lst= dao.find(sql, phone);
		if(lst.size()>0) flag=true;
		return flag;
	}
	
	/**
	 * 根据手机号获取用户
	 * @author liushaomin
	 * 2016年11月16日
	 * @param phone
	 * @return
	 */
	public static UserAppModel getByPhone(String phone) {
		String sql="select * from buyer a where a.phone='"+phone+"'";
		UserAppModel user = dao.findFirst(sql);
		return user;
	}
	
	/**
	 * 根据userid获取用户
	 * @author liushaomin
	 * 2017年3月21日
	 * @param userid
	 * @return
	 */
	public static UserAppModel getById(String userid) {
		String sql="select * from buyer a where a.user_id='"+userid+"'";
		UserAppModel user = dao.findFirst(sql);
		return user;
	}
	
	/**
	 * 判断验证码是否正确
	 * @author liushaomin
	 * 2016年10月13日
	 * @param phone
	 * @param smsinfo
	 * @return
	 */
	public static boolean chkAppSMS(String phone, String code, String type){
		boolean flag=false;
		String sql = "select * from phone_code where phone = '"+phone+"' and type = '"+ type +"' order by c_time desc LIMIT 1";
		PhoneCodeModel phonecode = new PhoneCodeModel().findFirst(sql);
		if(phonecode!=null){
			if(code.equals(phonecode.getStr("code"))){
				flag = true;
			}
		}else{
			flag=false;
		}
		return flag;
	}
	
	/**
	 * 获取用户 购物车-按店铺分页
	 * @author lifuqiang
	 * 2016-12-13
	 * @param phone
	 * @param pageNumber
	 * @return
	 */
	public Page<UserAppModel> shoppingcart(String phone, int pageNumber){
		String sql="select user_id, nick, avatar, phone ";
		String str = "from buyer b where user_id in (select seller_id from shoppingcart s,goods g where s.phone='"+phone+"' and s.goods_id = g.id) order by user_id";
		Page<UserAppModel> user_list=dao.paginate(pageNumber, Utils.pageSize, sql,str);
		return user_list;
	}
	
	/**
	 * 获取用户 购物车--未分页
	 * @author lifuqiang
	 * 2016-12-16
	 * @param phone
	 * @return
	 */
	public List<UserAppModel> shoppingcart(String phone){
		String sql="select user_id, phone from buyer b where user_id in (select seller_id from shoppingcart s,goods g where s.phone='"+phone+"' and s.goods_id = g.id) order by user_id";
		List<UserAppModel> user_list=dao.find(sql);
		return user_list;
	}
	
	/**
	 * 根据手机号得到id
	 * @author liushaomin
	 * 2017年2月28日
	 * @param phone
	 * @return
	 */
	public static int getUserid(String phone){
		UserAppModel user = dao.findFirst("select user_id from buyer where phone = '"+ phone +"'");
		return user.getInt("user_id");
	}

	
	/**
	 * 买到的宝贝——订单
	 * 获取每个订单的列表数量
	 * 0待付款(0,1),1待发货(2),2待收货(3),3待评价(5),4退款/货(8,10)
	 */
	public static Object getBuyersOrdernum(String phone) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < 5; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("columnName", i);
			if(i == 0){
				map.put("orderNumber", OrderInfoModel.queryIntByBuyer(phone,"0,1"));
			}else if (i == 1) {
				map.put("orderNumber", OrderInfoModel.queryIntByBuyer(phone,"2,8"));
			}else if (i == 2) {
				map.put("orderNumber", OrderInfoModel.queryIntByBuyer(phone,"3,10"));
			}else if (i == 3) {
				map.put("orderNumber", OrderInfoModel.queryIntByBuyer(phone,"5"));
			}else{
//				map.put("orderNumber", OrderInfoModel.queryIntByBuyer(phone,"8,10"));
				map.put("orderNumber", ReturnGoodsModel.queryIntByBuyer(phone));
			}
			list.add(map);
		}
		return list;
	}
	
	/**
	 * 商家 卖出的宝贝—— 获取每个订单的列表数量
	 * 0待发货(2),1已发货(3),2已完成(5,7,9,11),3退款/货(8,9,10,11)
	 */
	public static Object getSellerOrdernum(String phone) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < 4; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("columnName", i);
			if(i == 0){
				map.put("orderNumber", OrderInfoModel.queryIntBySeller(phone,"0,2"));
			}else if(i == 1){
				map.put("orderNumber", OrderInfoModel.queryIntBySeller(phone,"3"));
			}else if(i == 2){
				map.put("orderNumber", OrderInfoModel.queryIntBySeller(phone,"5,7,9,11,12"));
			}else{
//				map.put("orderNumber", OrderInfoModel.queryIntBySeller(phone,"8,9,10,11"));
				map.put("orderNumber", ReturnGoodsModel.queryIntBySeller(phone));
			}
			list.add(map);
		}
		return list;
	}
	
	/**
	 * 注册用户数量
	 * @author lifuqiang
	 * 2017-4-20
	 * @return
	 */
	public static long userNumber(){
		String sql = "select * from buyer";
		List<UserAppModel> userlist = dao.find(sql);
		return userlist.size();
	}
}
